package com.wakala.employment.adapter.base;

import android.util.Log;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ViewPage的 fragment适配器，实现了可以动态替换指定位置的fragment
 * @param <T>
 */
public class XFragmentAdapter<T extends Fragment> extends FragmentPagerAdapter {

    private String TAG = XFragmentAdapter.class.getSimpleName();

    private FragmentManager fm;
    private List<T> mFragmentList = new ArrayList<>();

    private List<String> mTitleList = new ArrayList<>();

    private boolean[] fragmentsUpdateFlag = {};

    public XFragmentAdapter(@NonNull FragmentManager fm) {
        super(fm);
        this.fm = fm;
    }

    public XFragmentAdapter(@NonNull FragmentManager fm, T[] fragments) {
        this(fm, Arrays.asList(fragments));
        this.fm = fm;
        this.fragmentsUpdateFlag = new boolean[fragments.length];
    }

    public XFragmentAdapter(@NonNull FragmentManager fm, List<T> fragments) {
        super(fm);
        this.fm = fm;
        this.fragmentsUpdateFlag = new boolean[fragments.size()];
        setFragments(fragments);
    }

    public XFragmentAdapter(@NonNull FragmentManager fm, int behavior) {
        super(fm, behavior);
    }

    public XFragmentAdapter(@NonNull FragmentManager fm, int behavior, T[] fragments) {
        this(fm, behavior, Arrays.asList(fragments));
        this.fm = fm;
        this.fragmentsUpdateFlag = new boolean[fragments.length];
    }

    public XFragmentAdapter(@NonNull FragmentManager fm, int behavior, List<T> fragments) {
        super(fm, behavior);
        setFragments(fragments);
        this.fm = fm;
        this.fragmentsUpdateFlag = new boolean[fragments.size()];
    }

    public XFragmentAdapter setFragments(List<T> fragments) {
        if (fragments != null && fragments.size() > 0) {
            mFragmentList.clear();
            mFragmentList.addAll(fragments);
        }
        this.fragmentsUpdateFlag = new boolean[fragments.size()];
        return this;
    }

    public XFragmentAdapter addFragments(List<T> fragments) {
        if (fragments != null && fragments.size() > 0) {
            mFragmentList.addAll(fragments);
        }
        this.fragmentsUpdateFlag = new boolean[fragments.size()];
        return this;
    }

    public XFragmentAdapter setTitles(List<String> titles) {
        if (titles != null && titles.size() > 0) {
            mTitleList.clear();
            mTitleList.addAll(titles);
        }
        return this;
    }

    public XFragmentAdapter addTitles(List<String> titles) {
        if (titles != null && titles.size() > 0) {
            mTitleList.addAll(titles);
        }
        return this;
    }

    public XFragmentAdapter addFragment(T fragment, String title) {
        if (fragment != null) {
            mFragmentList.add(fragment);
            mTitleList.add(title);
        }
        this.fragmentsUpdateFlag = new boolean[mFragmentList.size()];
        return this;
    }

    @NonNull
    @Override
    public T getItem(int position) {
        return mFragmentList.get(position);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        //得到缓存的fragment

        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        //以下代码是实现viewPage动态替换fragment的关键。
        if (fragment  instanceof Fragment) {


            //得到tag


            String fragmentTag = fragment.getTag();

            if (fragmentsUpdateFlag[position % fragmentsUpdateFlag.length]) {

                //如果这个fragment需要更新

                FragmentTransaction ft = fm.beginTransaction();

                //移除旧的fragment

                ft.remove(fragment);

                //换成新的fragment
                int index= position % mFragmentList.size();
                fragment = mFragmentList.get(index);

                //添加新fragment时必须用前面获得的tag ❶

                ft.add(container.getId(), fragment, fragmentTag);

                ft.attach(fragment);

                ft.commit();


                //复位更新标志

                fragmentsUpdateFlag[position % fragmentsUpdateFlag.length] = false;

            }
        }

        return fragment;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {

        for(int i=0;i<fragmentsUpdateFlag.length;i++){
            if(fragmentsUpdateFlag[i]){
                Log.i(TAG,"需要改变");
                return POSITION_NONE;
            }
        }

        return super.getItemPosition(object);

    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitleList.get(position);
    }

    public List<T> getFragmentList() {
        return mFragmentList;
    }

    public List<String> getTitleList() {
        return mTitleList;
    }

    public void  setUpdateFragment(int index,boolean b){
        this.fragmentsUpdateFlag[index] = b;
    }
}
